
PLUG.Platform = PLUG_PLATFORM_SHARED

PLUG.ClassName = "sh_mount"
PLUG.PrintName = "Plug Mount Control"
PLUG.Author = "thomasfn"

PLUG.Hard = true

if (SERVER) then

	function PLUG:DoConCommand( ply, com, args )
		if (com == "plugs_getlist") then
			if (!ply:HasPriv( "mount_plugs" )) then return end
			local plist = plugs.GetAll()
			local tmp = {}
			for k, v in pairs( plist ) do
				table.insert( tmp, { v.ClassName, v.PrintName, v.Author, v.Hard, v.Platform, plugs.IsInstanced( v.ClassName ) } )
			end
			datastream.StreamToClients( ply, "gmanage", { "plugslist", tmp } )
			return true
		end
		if (com == "plugs_mount") then
			if (!ply:HasPriv( "mount_plugs" )) then return end
			local name = args[1]
			if (!name) then return true end
			plugs.Mount( name )
			ply:Log( "plugs", "Mounted '" .. name .. "'" )
			ply:PrintMessage( HUD_PRINTTALK, "Plug mounted!" )
			plugs.CallHalt( "SendChatNotice", ply:Nick() .. " mounted " .. name .. "!", true )
			return true
		end
		if (com == "plugs_unmount") then
			if (!ply:HasPriv( "mount_plugs" )) then return end
			local name = args[1]
			if (!name) then return true end
			plugs.Unmount( name )
			ply:Log( "plugs", "Unmounted '" .. name .. "'" )
			ply:PrintMessage( HUD_PRINTTALK, "Plug unmounted!" )
			plugs.CallHalt( "SendChatNotice", ply:Nick() .. " unmounted " .. name .. "!", true )
			return true
		end
	end
	
	function PLUG:IsValidPriv( priv )
		if (priv == "mount_plugs") then return true end
	end

end

if (CLIENT) then

	local pmr_r_bgcol = Color( 128, 100, 100, 255 )
	local pmr_r_bgcol2 = Color( 156, 128, 128, 255 )
	local pmr_g_bgcol = Color( 100, 128, 100, 255 )
	local pmr_g_bgcol2 = Color( 128, 156, 128, 255 )
	local pmr_bgcol = Color( 100, 100, 100, 255 )
	local pmr_bgcol2 = Color( 128, 128, 128, 255 )
	function PLUG:Init()
		local PANEL = {}
		function PANEL:Setup( intname, extname, author, mounted, fixed )
			self.IntName = intname
			self.ExtName = extname
			self.Author = author
			self.Mounted = mounted
			self.Fixed = fixed
			if (fixed) then return end
			local img = vgui.Create( "DImage", self )
			if (mounted) then img:SetMaterial( "gmanage/ico_accept" ) else img:SetMaterial( "gmanage/ico_deny" ) end
			self.Img = img
			local btn = vgui.Create( "DButton", self )
			if (mounted) then btn:SetText( "Unmount" ) else btn:SetText( "Mount" ) end
			function btn:DoClick()
				if (mounted) then
					plugs.Call( "SendCommand", "plugs_unmount", intname )
				else
					plugs.Call( "SendCommand", "plugs_mount", intname )
				end
			end
			self.Btn = btn
		end
		function PANEL:Paint()
			local col
			if (self.Fixed) then
				col = pmr_bgcol
				if (self.Hovered) then col = pmr_bgcol2 end
			else
				if (self.Mounted) then
					col = pmr_g_bgcol
					if (self.Hovered) then col = pmr_g_bgcol2 end
				else
					col = pmr_r_bgcol
					if (self.Hovered) then col = pmr_r_bgcol2 end
				end
			end
			draw.RoundedBox( 6, 0, 0, self:GetWide(), self:GetTall(), col )
			draw.SimpleText( "Plug Name: " .. (self.ExtName or "Unknown"), "Default", 6, 6, color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP )
			draw.SimpleText( "Class Name: " .. (self.IntName or "Unknown"), "Default", 6, 24, color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP )
			draw.SimpleText( "Author: " .. (self.Author or "Unknown"), "Default", 6, 42, color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP )
		end
		function PANEL:PerformLayout()
			self:SetTall( 66 )
			if (self.Img) then
				self.Img:SetSize( self:GetTall() - 12, self:GetTall() - 12 )
				self.Img:SetPos( self:GetWide() - self.Img:GetWide() - 6, 6 )
				if (self.Btn) then
					self.Btn:SizeToContents()
					local w, h = self.Btn:GetSize()
					local x, y = self.Img:GetPos()
					self.Btn:SetSize( w+6, h+6 )
					self.Btn:SetPos( x - self.Btn:GetWide() - 6, 0 )
					self.Btn:CenterVertical()
				end
			end
		end
		vgui.Register( "PlugMountRow", PANEL, "PANEL" )
	end

	local SERVERWIN

	function PLUG:DataStream( tab )
		if (tab[1] == "plugslist") then
			self.ServerPlugs = tab[2]
			if (SERVERWIN && SERVERWIN:IsValid()) then
				plugs.Call( "BuildServerPanel", SERVERWIN, "plugcontrol" )
			end
		end
	end

	function PLUG:GetPrivDictionary()
		return { mount_plugs = "Mount Plugs" }
	end
	
	local newaction = { { "plugcontrol", "Mount/Unmount Plugs" } }
	function PLUG:GetServerActions()
		return newaction
	end
	
	function PLUG:BuildServerPanel( window, acname )
		if (acname == "plugcontrol") then
			SERVERWIN = window
		else
			SERVERWIN = nil
		end
	end
	
	local PlugRowTable
	
	function PLUG:BuildActionPanel( pn, action )
		if (action == "plugcontrol") then
			local w, h = pn:GetWide(), pn:GetTall()
			
			local plist = vgui.Create( "DPanelList", pn )
			plist:SetPos( 5, 5 )
			plist:SetSize( w-10, h-35 )
			plist:SetSpacing( 5 )
			plist:SetPadding( 5 )
			plist:EnableHorizontal( false )
			plist:EnableVerticalScrollbar( true )
			
			PlugRowTable = {}
			
			local used = {}
			for _, v in pairs( plugs.GetAll() ) do
				used[ v.ClassName ] = true
				local name = v.PrintName or "< No Name >"
				local author = v.Author or "Unknown"
				local row = vgui.Create( "PlugMountRow" )
				row:Setup( v.ClassName, name, author, plugs.IsInstanced( v.ClassName ), v.Hard )
				plist:AddItem( row )
				table.insert( PlugRowTable, row )
			end
			
			if (self.ServerPlugs) then
				for _, v in pairs( self.ServerPlugs ) do
					if (!used[ v[1] ]) then
						local name = v[2] or "< No Name >"
						local author = v[3] or "Unknown"
						local row = vgui.Create( "PlugMountRow" )
						row:Setup( v[1], name, author, v[6], v[4] )
						plist:AddItem( row )
						table.insert( PlugRowTable, row )
					end
				end
			end
			
			local btn = vgui.Create( "DButton", pn )
			btn:SetPos( 5, h-25 )
			btn:SetText( "Get Serverside Plugs" )
			btn:SetSize( w*0.5 - 10, 20 )
			function btn:DoClick() plugs.Call( "SendCommand", "plugs_getlist" ) end
		end
	end
	
	function PLUG:PlugMounted( name )
		for _, row in pairs( PlugRowTable ) do
			if (row && row:IsValid()) then
				if (row.IntName == name) then
					if (row.Img) then row.Img:Remove() end
					if (row.Btn) then row.Btn:Remove() end
					row:Setup( name, row.ExtName, row.Author, true, row.Fixed )
					row:PerformLayout()
				end
			end
		end
	end
	
	function PLUG:PlugUnmounted( name )
		for _, row in pairs( PlugRowTable ) do
			if (row && row:IsValid()) then
				if (row.IntName == name) then
					if (row.Img) then row.Img:Remove() end
					if (row.Btn) then row.Btn:Remove() end
					row:Setup( name, row.ExtName, row.Author, false, row.Fixed )
					row:PerformLayout()
				end
			end
		end
	end

end